<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="applyVehicle">申请</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="标题"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="applyId" stripe remoteSort remoteFilter>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="用车标题" prop="title" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="车辆类型" prop="typeName" width="100"></el-table-column>
				<el-table-column label="车牌号" prop="vehicleNumber" width="100"></el-table-column>
				<el-table-column label="用车人" prop="usedUserName" width="100"></el-table-column>
				<el-table-column label="审批状态" prop="status" width="100">
					<template #default="scope">
						<el-tag type="warning" effect="dark" v-if="scope.row.status=='0'">审批中</el-tag>
						<el-tag type="success" effect="dark" v-else-if="scope.row.status=='1'">审批通过</el-tag>
						<el-tag type="info" effect="dark" v-else-if="scope.row.status=='2'">审批未通过</el-tag>
						<el-tag type="danger" effect="dark" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="用车时间" prop="beginTime" width="150"></el-table-column>
				<el-table-column label="里程" prop="mileage" width="150">
					<template #default="scope">
						{{scope.row.mileage}}/公里
					</template>
				</el-table-column>
				<el-table-column label="出发地点" prop="sourceAddress" width="200"></el-table-column>
				<el-table-column label="目的地" prop="destination" width="200"></el-table-column>
				<el-table-column label="同行人员" prop="passenger" width="100">
					<template #default="scope">
						{{scope.row.passenger}}/人
					</template>
				</el-table-column>
				<el-table-column label="申请时间" prop="createTime" width="150"></el-table-column>
				<el-table-column label="用车事由" prop="cause" width="300"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-if="scope.row.status=='0'">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="scope.row.status=='0'">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<dispatch-details v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></dispatch-details>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import dispatchDetails from '@/views/office/vehicle/dispatch/details'
import saveDialog from './save'
export default {
	name: 'Vehicle Apply',
	components: {
		saveDialog,
		scSelectFilter,
		dispatchDetails
	},
	data() {
		return {
			dialog: {
				save: false,
				detailsSave:false,
			},
			filterData: [
				{
					title: "申请时间",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "车辆类型",
					key: "type",
					multiple: false,
					options:[{
						label: "全部",
						value: ""
					}]
				},
				{
					title: "审批状态",
					key: "status",
					multiple: false,
					options: [{
						value: '',
						label: '全部'
					},
						{
							label: "审批中",
							value: "0"
						},
						{
							label: "审批通过",
							value: "1"
						},
						{
							label: "审批未通过",
							value: "2"
						}],
				}
			],
			apiObj: this.$API.office.vehicle.getVehicleApprovedList,
			search: {
				date:'',
				type:'',
				status:'',
				keyword: null
			}
		}
	},
	mounted() {
		this.getSysDicByCode();
	},
	methods: {
		async getSysDicByCode()
		{
			let res = await this.$API.system.sysDic.getSysDicByCode.get({code:'vehicleType'});
			if (res.code == 200) {
				this.filterData[1].options=this.filterData[1].options.concat(res.data);
			}
		},
		//添加
		applyVehicle() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.detailsSave = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open().setData(row)
			})
		},
		//搜索
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		filterChange(data){
			this.search.date = data.date;
			this.search.type = data.type
			this.search.status = data.status;
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
